/**
 * Created by Administrator on 2017/5/25.
 */

var express = require('express');
var router = express.Router();
var crypto = require('crypto');
var adminDao = require('../dao/admin.js');
var AdminToken = require('../db/user_tokens.js');
var AdminRouter = module.exports;
var Log = require('../dao/log.js');

/**
 * 管理员登录
 * @param  account  password
 *         url/admin/login
 */

AdminRouter.login = function (req, res) {
    var par = paramAll(req);
    if(!par.account) {
        res.json(new ERR('用户名不能为空！',400));
        return;
    }
    if(!par.password) {
        res.json(new ERR('密码不能为空！', 400));
        return;
    } else {
        par.password = 'tao1024' + par.password;
        var md5=crypto.createHash('md5');
        md5.update(par.password);
        par.password = md5.digest('hex');
    }

    var adminInfo = {};
    adminInfo.account = par.account;
    adminInfo.password = par.password;
    adminDao.login(adminInfo, function(err, data){
        if (err) {
            adminInfo.adm_uid = 0;
            adminInfo.type = 0;
            adminInfo.detail = err;
            Log.adminLog(adminInfo,function (result) {
                if(result) {
                    res.json(new ERR(err, 400));
                }
            });
        } else {
            adminInfo.uid = data.uid;
            adminInfo.name = data.name;
            adminInfo.isAdmin = true;
            adminInfo.role = data.role;
            adminInfo.app_sid = data.app_sid;
            AdminToken.onLogin(adminInfo ,function(err, data){
                if(err) {
                    console.log(err);
                    return;
                }
                adminInfo.adm_uid = adminInfo.uid;
                delete adminInfo.uid;
                adminInfo.token = data;
                adminInfo.type = 0; //登录
                adminInfo.detail = '登录成功';
                Log.adminLog(adminInfo,function (result) {
                    if (result) {
                        delete adminInfo.password;
                        delete adminInfo.type;
                        delete adminInfo.detail;
                        adminInfo.uid = adminInfo.adm_uid;
                        delete adminInfo.adm_uid;
                        res.json(new PKG(adminInfo));
                    }
                });
            });
        }
    });
}

router.post('/login',AdminRouter.login);

/**
 * 用户修改密码
 * http://<tao_server:port>/admin/resetPwd
 * @param  token   password
 */
AdminRouter.resetPwd = function (req, res) {
    var par = paramAll(req);
    AdminToken.checkAdminToken(par.token,function (err, statusCode ,loginInfo) {
        if (err) {
            res.json(new ERR(err, statusCode));
            return;
        }
        var adminInfo = {};
        adminInfo.adm_uid = loginInfo.uid;
        if(par.app_sid){
            adminInfo.app_sid = par.app_sid;
        }
        adminInfo.type = 1; //日志修改密码
        if (!par.oldpassword) return res.json(new ERR('旧密码不能为空', 400));
        if (!par.password) return res.json(new ERR('新密码不能为空', 400));


        adminInfo.oldpassword = encryPassword(par.oldpassword);
        adminInfo.password = encryPassword(par.password);
        adminDao.isPassword(adminInfo, function (err, data) {
            if (err) {
                res.json(new ERR(err, 400));
                return;
            }
            adminInfo.account = data.account;
            adminDao.resetPwd(adminInfo, function (err) {
                if (err) {
                    adminInfo.detail = err;
                    Log.userLog(adminInfo, function (result) {
                        if (result) {
                            console.log(err);
                            res.json(new ERR(err, 400));
                        }
                    });
                    return;
                }
                delete adminInfo.password;
                delete adminInfo.oldpassword;
                AdminToken.updateToToken(adminInfo, function (e, result) {
                    if (e) {
                        adminInfo.detail = e;
                        Log.adminLog(AdminToken, function (data) {
                            if (data) {
                                res.json(new ERR('修改密码失败', 400));
                            }
                        });
                    } else {
                        adminInfo.detail = '重置密码成功！';
                        Log.adminLog(adminInfo, function (data) {
                            if (data) {
                                delete adminInfo.type;
                                adminInfo.token = result;
                                res.json(new PKG(adminInfo));
                            }
                        });
                    }
                });
            });
        });
    });
}

router.post('/resetPwd', AdminRouter.resetPwd);

/**
 * 管理员退出登录
 * @param  token
 *         url/admin/logout
 */

AdminRouter.logout = function(req, res){
    var par = paramAll(req);
    var token = par.token;

    AdminToken.onLogout(token ,function(err, data){
        if(err) {
            return;
        }
        if(data) {
            res.json(new PKG('退出成功！'));
        } else {
            res.json(new ERR('failed',400));
        }
    });
}

router.post('/logout', AdminRouter.logout);

module.exports = router;